package guicommon.controls;

import guicommon.gui.ControlMessage;
import guicommon.gui.TextAreaMessage;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JTextArea;

public class TextArea extends Control {

    private JTextArea textBox;

    public void SetText(String text) {
        Update("SetText", text);
        textBox.setText(text);
    }

    public void AppendText(String text) {
        String temp = textBox.getText();
        String[] res = temp.split(String.format("%n"));
        if (res.length == 3) {
            textBox.setText("");
            textBox.append(res[1]);
            textBox.append(String.format("%n"));
            textBox.append(res[2]);
            textBox.append(String.format("%n"));
        }
        Update("AppendText", text);
        textBox.append(text);
        textBox.append(String.format("%n"));
    }

    public TextArea() {
        textBox = new JTextArea();
        textBox.setFont(new Font("Calibry", 0, 12));
    }

    public String GetText() {
        return textBox.getText();
    }

    @Override
    public ControlMessage ToMessage() {
        TextAreaMessage result = new TextAreaMessage();
        SetCommonFields(result);
        result.SetText(GetText());
        return result;
    }

    protected Component GetComponent() {
        return textBox;
    }
}
